package com.alinks.modbus.core.utils;

/**
 * following class implements longitudinal redundancy checkFrame.
 */
public final class LRC {
    static public int calc(byte[] b) {
        int lrc = 0;

        for (byte aB : b) {
            lrc += aB;
        }
        return (byte) (-lrc) & 0xff;
    }

    public static void main(String[] args) {
        //0d 0a
//        byte[] bytes = new byte[]{ 0x30, 0x31, 0x30, 0x33, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x41};
        byte[] bytes = new byte[]{ 0x01, 0x03, 0x00, 0x00, 0x00, 0x0A};
        int lrc = calc(bytes);
        System.out.println(Integer.toHexString(lrc));
        System.out.println(Integer.toHexString(lrc & 0xff));
        System.out.println(Integer.toHexString(lrc >> 8 & 0xff));
    }
}
